import pagination from '@/components/pagination.vue'
import { Request, confirm, successToast, errorToast, getDate, parseTime, checkPermission } from '@/utils'

let request = new Request()

export default {
	
	data () {
		return {
			filter : {
				date : []
			}, 
			
			pageQuery : {
				date : []
			},
			logList : [],
			downloadLoading : false
		}
	},
	
	components : {
		pagination
	},
	
	methods : {
		checkPermission : checkPermission,
		search : function(){
			let filter = Object.assign({}, this.filter)
			
			let date = filter.date
			
			if(date){
				filter.start = date[0]
				filter.end = date[1]
			}
			
			if(filter.searchType == 1) {
				filter.orderNo = filter.searchKey
			}else if(filter.searchType == 2) {
				filter.deviceNo = filter.searchKey
			}
			
			delete filter.date
			delete filter.searchType
			delete filter.searchKey
			
			this.pageQuery = filter
			
			this.$nextTick(() => {
				this.$refs.pagination.initPage()
			})

		},
		
		page : function(rows){
			this.logList.splice(0)
			rows.forEach(v => {
				this.logList.push(v)
			})
		},
		
		handleDownload() { //导出
			this.downloadLoading = true
			var params = Object.assign({},this.pageQuery)
			params.pageSize = 100000
			request.get('Finance/dayIncome',res=>{
				var list = []
				this.downloadLoading = false
				if(res.success){
					try{
						let rows = res.data.list
						rows.forEach(v=>{
							var item = [v.day, v.wx_charge, v.online_card_charge, v.amount]
							list.push(item)
						})
						import('@/vendor/Export2Excel').then(excel => {
							try{
								const tHeader = ['日期', '微信充电收入', '线上卡充电', '总金额']
								excel.export_json_to_excel({
									header: tHeader,
									data : list,
									filename: "日收入明细",
								})
							}catch(e){
								console.log(e.message)
							}
						}).catch(e => {
							console.log(e.message)
						})
					}catch(e){
						console.log(e.message)
					}
				}else{
					errorToast(res.msg, this)
				}
				
				
			}, params)
			
		},
	},
	
	created () {
		let d = getDate(-7)
		let d1 = getDate()
		this.filter.date = [d, d1]
		this.pageQuery.start = d
		this.pageQuery.end = d1
	}
}